<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    
    <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="generator" content="Hugo 0.56.3 with theme Tranquilpeak 0.4.7-BETA">
<meta name="author" content="iFence">
<meta name="keywords" content="">
<meta name="description" content="背景 在kafka的使用、测试过程中，可能老是会遇到想要删除某个主题的需求，但是由于对其内部机制不够了解，总是无法有效的删除主题。今天这篇文章详细讨论如何彻底的删除一个主题。 删除主题的前提 在kafka的broker端的配置参数中有这样一个参数：delete.topic.enable">


<meta property="og:description" content="背景 在kafka的使用、测试过程中，可能老是会遇到想要删除某个主题的需求，但是由于对其内部机制不够了解，总是无法有效的删除主题。今天这篇文章详细讨论如何彻底的删除一个主题。 删除主题的前提 在kafka的broker端的配置参数中有这样一个参数：delete.topic.enable">
<meta property="og:type" content="article">
<meta property="og:title" content="Kafka删除主题详解">
<meta name="twitter:title" content="Kafka删除主题详解">
<meta property="og:url" content="https://ifence.gitee.io/2019/08/kafka%E5%88%A0%E9%99%A4%E4%B8%BB%E9%A2%98%E8%AF%A6%E8%A7%A3/">
<meta property="twitter:url" content="https://ifence.gitee.io/2019/08/kafka%E5%88%A0%E9%99%A4%E4%B8%BB%E9%A2%98%E8%AF%A6%E8%A7%A3/">
<meta property="og:site_name" content="iFence&#39;s Nest">
<meta property="og:description" content="背景 在kafka的使用、测试过程中，可能老是会遇到想要删除某个主题的需求，但是由于对其内部机制不够了解，总是无法有效的删除主题。今天这篇文章详细讨论如何彻底的删除一个主题。 删除主题的前提 在kafka的broker端的配置参数中有这样一个参数：delete.topic.enable">
<meta name="twitter:description" content="背景 在kafka的使用、测试过程中，可能老是会遇到想要删除某个主题的需求，但是由于对其内部机制不够了解，总是无法有效的删除主题。今天这篇文章详细讨论如何彻底的删除一个主题。 删除主题的前提 在kafka的broker端的配置参数中有这样一个参数：delete.topic.enable">
<meta property="og:locale" content="zh-cn">

  
    <meta property="article:published_time" content="2019-08-31T08:29:01">
  
  
    <meta property="article:modified_time" content="2019-08-31T08:29:01">
  
  
  
    
      <meta property="article:section" content="Kafka">
    
  
  
    
      <meta property="article:tag" content="Kafka">
    
  


<meta name="twitter:card" content="summary">











  <meta property="og:image" content="https://avatars1.githubusercontent.com/u/53120100?s=460&v=4">
  <meta property="twitter:image" content="https://avatars1.githubusercontent.com/u/53120100?s=460&v=4">


    <title>Kafka删除主题详解</title>

    <link rel="icon" href="https://ifence.gitee.io/favicon.png">
    

    

    <link rel="canonical" href="https://ifence.gitee.io/2019/08/kafka%E5%88%A0%E9%99%A4%E4%B8%BB%E9%A2%98%E8%AF%A6%E8%A7%A3/">

    
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha256-eZrrJcwDc/3uDhsdt61sL2oOBY362qM3lon1gyExkL0=" crossorigin="anonymous" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.4/jquery.fancybox.min.css" integrity="sha256-vuXZ9LGmmwtjqFX1F+EKin1ThZMub58gKULUyf0qECk=" crossorigin="anonymous" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.4/helpers/jquery.fancybox-thumbs.min.css" integrity="sha256-SEa4XYAHihTcEP1f5gARTB2K26Uk8PsndQYHQC1f4jU=" crossorigin="anonymous" />
    
    
    <link rel="stylesheet" href="https://ifence.gitee.io/css/style-twzjdbqhmnnacqs0pwwdzcdbt8yhv8giawvjqjmyfoqnvazl0dalmnhdkvp7.min.css" />
    
    

    
      
<script type="application/javascript">
var doNotTrack = false;
if (!doNotTrack) {
	(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
	(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
	m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
	})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
	ga('create', 'UA-123-45', 'auto');
	
	ga('send', 'pageview');
}
</script>

    
    
  </head>

  <body>
    <div id="blog">
      <header id="header" data-behavior="5">
  <i id="btn-open-sidebar" class="fa fa-lg fa-bars"></i>
  <div class="header-title">
    <a class="header-title-link" href="https://ifence.gitee.io/">iFence&#39;s Nest</a>
  </div>
  
    
      <a class="header-right-picture "
         href="https://ifence.gitee.io/#about">
    
    
    
      
        <img class="header-picture" src="https://avatars1.githubusercontent.com/u/53120100?s=460&amp;v=4" alt="作者的图片" />
      
    
    </a>
  
</header>

      <nav id="sidebar" data-behavior="5">
  <div class="sidebar-container">
    
      <div class="sidebar-profile">
        <a href="https://ifence.gitee.io/#about">
          <img class="sidebar-profile-picture" src="https://avatars1.githubusercontent.com/u/53120100?s=460&amp;v=4" alt="作者的图片" />
        </a>
        <h4 class="sidebar-profile-name">iFence</h4>
        
          <h5 class="sidebar-profile-bio">虽千万人吾往矣</h5>
        
      </div>
    
    <ul class="sidebar-buttons">
      
  <li class="sidebar-button">
    
      <a class="sidebar-button-link " href="https://ifence.gitee.io/">
    
      <i class="sidebar-button-icon fa fa-lg fa-home"></i>
      
      <span class="sidebar-button-desc">首页</span>
    </a>
  </li>

  <li class="sidebar-button">
    
      <a class="sidebar-button-link " href="https://ifence.gitee.io/archives">
    
      <i class="sidebar-button-icon fa fa-lg fa-archive"></i>
      
      <span class="sidebar-button-desc">归档</span>
    </a>
  </li>

  <li class="sidebar-button">
    
      <a class="sidebar-button-link " href="https://ifence.gitee.io/tags">
    
      <i class="sidebar-button-icon fa fa-lg fa-tags"></i>
      
      <span class="sidebar-button-desc">标签</span>
    </a>
  </li>

  <li class="sidebar-button">
    
      <a class="sidebar-button-link " href="https://ifence.gitee.io/categories">
    
      <i class="sidebar-button-icon fa fa-lg fa-bookmark"></i>
      
      <span class="sidebar-button-desc">分类</span>
    </a>
  </li>

  <li class="sidebar-button">
    
      <a class="sidebar-button-link " href="https://ifence.gitee.io/about">
    
      <i class="sidebar-button-icon fa fa-lg fa-user"></i>
      
      <span class="sidebar-button-desc">关于</span>
    </a>
  </li>


    </ul>
    <ul class="sidebar-buttons">
      
  <li class="sidebar-button">
    
      <a class="sidebar-button-link " href="https://github.com/iFence" target="_blank" rel="noopener">
    
      <i class="sidebar-button-icon fa fa-lg fa-github"></i>
      
      <span class="sidebar-button-desc">GitHub</span>
    </a>
  </li>

  <li class="sidebar-button">
    
      <a class="sidebar-button-link " href="https://blog.csdn.net/Vector97" target="_blank" rel="noopener">
    
      <i class="sidebar-button-icon fa fa-lg fa-codiepie"></i>
      
      <span class="sidebar-button-desc">CSDN</span>
    </a>
  </li>

  <li class="sidebar-button">
    
      <a class="sidebar-button-link " href="https://www.jianshu.com/u/424596d51c5a" target="_blank" rel="noopener">
    
      <i class="sidebar-button-icon fa fa-lg fa-gratipay"></i>
      
      <span class="sidebar-button-desc">简书</span>
    </a>
  </li>


    </ul>
    <ul class="sidebar-buttons">
      

    </ul>
  </div>
</nav>

      

      <div id="main" data-behavior="5"
        class="
               hasCoverMetaIn
               ">
        <article class="post" itemscope itemType="http://schema.org/BlogPosting">
          
          
            <div class="post-header main-content-wrap text-left">
  
    <h1 class="post-title" itemprop="headline">
      Kafka删除主题详解
    </h1>
  
  
  <div class="postShorten-meta post-meta">
    
      <time itemprop="datePublished" datetime="2019-08-31T08:29:01&#43;08:00">
        
  八月 31, 2019

      </time>
    
    
  
  
    <span>发布在</span>
    
      <a class="category-link" href="https://ifence.gitee.io/categories/kafka">Kafka</a>
    
  

  </div>

</div>
          
          <div class="post-content markdown" itemprop="articleBody">
            <div class="main-content-wrap">
              

<h2 id="背景">背景</h2>

<p>在kafka的使用、测试过程中，可能老是会遇到想要删除某个主题的需求，但是由于对其内部机制不够了解，总是无法有效的删除主题。今天这篇文章详细讨论如何彻底的删除一个主题。</p>

<h2 id="删除主题的前提">删除主题的前提</h2>

<p>在kafka的broker端的配置参数中有这样一个参数：<code>delete.topic.enable</code>，必须将它设置为true才能够删除kafka主题。这个参数的默认值是true。如果配置为false那么删除主题的命令将会被忽略。</p>

<p>kafka仅支持删除用户自定义主题，kafka的内置主题，如保存偏移量的主题<code>__consumer_offset_</code>不允许被删除，如果执行删除命令会报错。</p>

<p>对于不存在的主题如果删除同样会抛出错误信息</p>

<h2 id="使用删除主题命令的本质">使用删除主题命令的本质</h2>

<p><strong>使用<code>kafka-topics.sh</code>脚本删除主题的行为本质上只是在zk中的<code>/admin/delete_topics</code>路径下创建一个待删除主题的同名节点，以此标记该主题的待删除状态。</strong>所以我们可以在zk操作删除主题</p>

<p>当我们执行下面kafka删除主题的命令：</p>

<pre><code class="language-bash">$&gt;./bin/kafka-topics --delete --topic test-1 --zookeeper master:2181/kafka
</code></pre>

<p>再查询kafka的主题：</p>

<pre><code class="language-bash">[root@localhost kafka_2.11-0.10.2.2]$&gt; ./bin/kafka-topics.sh --list --zookeeper localhost:2181/kafka   
__consumer_offsets
my-topic
test-1 - marked for deletion #发现主题仅仅是标记删除了
</code></pre>

<p>那么上面的删除命令到底发生了什么呢？</p>

<p>执行上述删除主题命令以后，会在zookeeper的<code>/admin/delete_topics</code>路径下创建一个与删除主题同名的节点</p>

<pre><code class="language-bash">[zk: localhost:2181(CONNECTED) 4] ls /kafka/admin/delete_topics
[test-1]
</code></pre>

<p>了解原理以后，我们在zk的该目录下手动创建要删除的节点，看一下会发生什么</p>

<pre><code class="language-bash">[zk: localhost:2181(CONNECTED) 6] create /kafka/admin/delete_topics/test-2 &quot;&quot;
Created /kafka/admin/delete_topics/test-2 #手动创建要删除的节点成功
</code></pre>

<p>此时再次查询kafka的主题信息</p>

<pre><code class="language-bash">[root@localhost kafka_2.11-0.10.2.2]$ ./bin/kafka-topics.sh --list --zookeeper localhost:2181/kafka
__consumer_offsets
my-topic
test-1 - marked for deletion
test-2 - marked for deletion #发现使用zk命令删除的节点与test-1是同样的效果
</code></pre>

<h2 id="如何彻底删除一个kafka主题">如何彻底删除一个kafka主题</h2>

<p>通过上述说明我们已经知道使用kafka的删除命令是无法做到真正删除一个已经存在的主题的，那么该如何彻底删除主题呢？</p>

<p>一个kafka主题包含两部分内容：存储在zk中的元数据信息，和存储在磁盘<code>log.dir</code>或者<code>logs.dir</code>下的主题实际存储数据。</p>

<p>前者存储在zk的<code>/kafka/config/topics/</code>和<code>/kafka/brokers/topics</code>下，只需要删除zk中对应主题的节点即可（以test-1）为例</p>

<pre><code class="language-bash">[zk: master:2181/kafka (CONNECTED) 1] rmr /config/topics/test-1
[zk: master:2181/kafka (CONNECTED) 2] rmr /brokers/topics/test-1
</code></pre>

<p>后者找到kafka配置的数据目录，将其删除即可</p>

<pre><code class="language-bash">#该文件的目录配置在server.properties中
$&gt;rm -rf /opt/modules/kafka-logs/test-1
</code></pre>

<p>执行上述操作以后，test-1主题的所有数据就被彻底删掉了，我们再进行查询发现已经没有test-1了</p>

<pre><code class="language-bash">[root@localhost kafka_2.11-0.10.2.2]# ./bin/kafka-topics.sh --list --zookeeper localhost:2181/kafka
__consumer_offsets
my-topic
test-2 - marked for deletion
</code></pre>

              
            </div>
          </div>
          <div id="post-footer" class="post-footer main-content-wrap">
            
              
                
                
                  <div class="post-footer-tags">
                    <span class="text-color-light text-small">标签</span><br/>
                    
  <a class="tag tag--primary tag--small" href="https://ifence.gitee.io/tags/kafka/">Kafka</a>

                  </div>
                
              
            
            <div class="post-actions-wrap">
  
      <nav >
        <ul class="post-actions post-action-nav">
          
            <li class="post-action">
              
                <a class="post-action-btn btn btn--default tooltip--top" href="https://ifence.gitee.io/2019/09/scala%E4%B8%ADcontinue%E5%92%8Cbreak%E7%9A%84%E5%87%A0%E7%A7%8D%E5%AE%9E%E7%8E%B0%E6%96%B9%E5%BC%8F/" data-tooltip="Scala中continue和break的几种实现方式">
              
                  <i class="fa fa-angle-left"></i>
                  <span class="hide-xs hide-sm text-small icon-ml">下一篇</span>
                </a>
            </li>
            <li class="post-action">
              
                <a class="post-action-btn btn btn--default tooltip--top" href="https://ifence.gitee.io/2019/08/log4j%E7%9A%84api%E4%BD%BF%E7%94%A8%E8%AF%A6%E8%A7%A3/" data-tooltip="Log4j的api使用详解">
              
                  <span class="hide-xs hide-sm text-small icon-mr">上一篇</span>
                  <i class="fa fa-angle-right"></i>
                </a>
            </li>
          
        </ul>
      </nav>
    <ul class="post-actions post-action-share" >
      
        <li class="post-action hide-lg hide-md hide-sm">
          <a class="post-action-btn btn btn--default btn-open-shareoptions" href="#btn-open-shareoptions">
            <i class="fa fa-share-alt"></i>
          </a>
        </li>
        
      
      
        <li class="post-action">
          <a class="post-action-btn btn btn--default" href="#disqus_thread">
            <i class="fa fa-comment-o"></i>
          </a>
        </li>
      
      <li class="post-action">
        
          <a class="post-action-btn btn btn--default" href="#">
        
          <i class="fa fa-list"></i>
        </a>
      </li>
    </ul>
  
</div>

            
              
                <div id="disqus_thread">
  <noscript>Please enable JavaScript to view the <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
</div>
              
            
          </div>
        </article>
        <footer id="footer" class="main-content-wrap">
  <span class="copyrights">
    &copy; 2019 iFence. All Rights Reserved
  </span>
</footer>

      </div>
      <div id="bottom-bar" class="post-bottom-bar" data-behavior="5">
        <div class="post-actions-wrap">
  
      <nav >
        <ul class="post-actions post-action-nav">
          
            <li class="post-action">
              
                <a class="post-action-btn btn btn--default tooltip--top" href="https://ifence.gitee.io/2019/09/scala%E4%B8%ADcontinue%E5%92%8Cbreak%E7%9A%84%E5%87%A0%E7%A7%8D%E5%AE%9E%E7%8E%B0%E6%96%B9%E5%BC%8F/" data-tooltip="Scala中continue和break的几种实现方式">
              
                  <i class="fa fa-angle-left"></i>
                  <span class="hide-xs hide-sm text-small icon-ml">下一篇</span>
                </a>
            </li>
            <li class="post-action">
              
                <a class="post-action-btn btn btn--default tooltip--top" href="https://ifence.gitee.io/2019/08/log4j%E7%9A%84api%E4%BD%BF%E7%94%A8%E8%AF%A6%E8%A7%A3/" data-tooltip="Log4j的api使用详解">
              
                  <span class="hide-xs hide-sm text-small icon-mr">上一篇</span>
                  <i class="fa fa-angle-right"></i>
                </a>
            </li>
          
        </ul>
      </nav>
    <ul class="post-actions post-action-share" >
      
        <li class="post-action hide-lg hide-md hide-sm">
          <a class="post-action-btn btn btn--default btn-open-shareoptions" href="#btn-open-shareoptions">
            <i class="fa fa-share-alt"></i>
          </a>
        </li>
        
      
      
        <li class="post-action">
          <a class="post-action-btn btn btn--default" href="#disqus_thread">
            <i class="fa fa-comment-o"></i>
          </a>
        </li>
      
      <li class="post-action">
        
          <a class="post-action-btn btn btn--default" href="#">
        
          <i class="fa fa-list"></i>
        </a>
      </li>
    </ul>
  
</div>

      </div>
      <div id="share-options-bar" class="share-options-bar" data-behavior="5">
  <i id="btn-close-shareoptions" class="fa fa-close"></i>
  <ul class="share-options">
    
  </ul>
</div>
<div id="share-options-mask" class="share-options-mask"></div>
    </div>
    
    <div id="about">
  <div id="about-card">
    <div id="about-btn-close">
      <i class="fa fa-remove"></i>
    </div>
    
      <img id="about-card-picture" src="https://avatars1.githubusercontent.com/u/53120100?s=460&amp;v=4" alt="作者的图片" />
    
    <h4 id="about-card-name">iFence</h4>
    
      <div id="about-card-bio">虽千万人吾往矣</div>
    
    
      <div id="about-card-job">
        <i class="fa fa-briefcase"></i>
        <br/>
        大数据开发
      </div>
    
    
      <div id="about-card-location">
        <i class="fa fa-map-marker"></i>
        <br/>
        中国 山东
      </div>
    
  </div>
</div>

    

    
  
    
      <div id="cover" style="background-image:url('https://ifence.gitee.io/images/cover.jpg');"></div>
    
  


    
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js" integrity="sha256-/BfiIkHlHoVihZdc6TFuj7MmJ0TWcWsMXkeDFwhi0zw=" crossorigin="anonymous"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.7/js/jquery.fancybox.min.js" integrity="sha256-GEAnjcTqVP+vBp3SSc8bEDQqvWAZMiHyUSIorrWwH50=" crossorigin="anonymous"></script>


<script src="https://ifence.gitee.io/js/script-pcw6v3xilnxydl1vddzazdverrnn9ctynvnxgwho987mfyqkuylcb1nlt.min.js"></script>


<script lang="javascript">
window.onload = updateMinWidth;
window.onresize = updateMinWidth;
document.getElementById("sidebar").addEventListener("transitionend", updateMinWidth);
function updateMinWidth() {
  var sidebar = document.getElementById("sidebar");
  var main = document.getElementById("main");
  main.style.minWidth = "";
  var w1 = getComputedStyle(main).getPropertyValue("min-width");
  var w2 = getComputedStyle(sidebar).getPropertyValue("width");
  var w3 = getComputedStyle(sidebar).getPropertyValue("left");
  main.style.minWidth = `calc(${w1} - ${w2} - ${w3})`;
}
</script>

<script>
$(document).ready(function() {
  hljs.configure({ classPrefix: '', useBR: false });
  $('pre.code-highlight > code, pre > code').each(function(i, block) {
    if (!$(this).hasClass('codeblock')) {
      $(this).addClass('codeblock');
    }
    hljs.highlightBlock(block);
  });
});
</script>


  
    
      <script>
        var disqus_config = function () {
          this.page.url = 'https:\/\/ifence.gitee.io\/2019\/08\/kafka%E5%88%A0%E9%99%A4%E4%B8%BB%E9%A2%98%E8%AF%A6%E8%A7%A3\/';
          
            this.page.identifier = '\/2019\/08\/kafka%E5%88%A0%E9%99%A4%E4%B8%BB%E9%A2%98%E8%AF%A6%E8%A7%A3\/'
          
        };
        (function() {
          
          
          if (window.location.hostname == "localhost") {
            return;
          }
          var d = document, s = d.createElement('script');
          var disqus_shortname = 'hugo-tranquilpeak-theme';
          s.src = '//' + disqus_shortname + '.disqus.com/embed.js';

          s.setAttribute('data-timestamp', +new Date());
          (d.head || d.body).appendChild(s);
        })();
      </script>
    
  




    
  </body>
</html>

